package com.ll.domain.yhj.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ll.app.api.yhj.AddUploadFileReqDTO;
import com.ll.app.api.yhj.PageFileReqDTO;
import com.ll.base.constant.LlConstants;
import com.ll.base.dto.ApiResultDTO;
import com.ll.base.dto.PageResultDTO;
import com.ll.base.enums.ResultCodeEnum;
import com.ll.base.exception.CustomRunTimeException;
import com.ll.base.util.ScalarUtil;
import com.ll.dal.mapper.YhjFileMapper;
import com.ll.dal.po.ShopPO;
import com.ll.dal.po.UserPO;
import com.ll.dal.po.YhjFilePO;
import com.ll.domain.shop.convert.ShopConvert;
import com.ll.domain.yhj.biz.YhjDomainService;
import com.ll.domain.yhj.enums.FileTypeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class YhjDomainServiceImpl implements YhjDomainService {

    private final YhjFileMapper yhjFileMapper;
    private final ApplicationEventPublisher applicationEventPublisher;
    /**
     * 排名前5的需要通知到大群
     */
    private final Integer NOTIFY_RANKING = 5;
    private final Integer RANKING_INTEGRAL = 20;


    @Override
    public ApiResultDTO pageFile(PageFileReqDTO reqDTO) {

        reqDTO.initPage();
        IPage iPage = new Page(reqDTO.getPageNo(), reqDTO.getPageSize());
        iPage = yhjFileMapper.selectPage(iPage, new LambdaQueryWrapper<YhjFilePO>()
        .like(ScalarUtil.isNotEmpty(reqDTO.getName()),YhjFilePO::getName, reqDTO.getName())
        .eq(reqDTO.getType()!=null,YhjFilePO::getType, reqDTO.getType())
        );
        PageResultDTO<ShopPO> poPageResult = new PageResultDTO<>(iPage.getRecords(), iPage.getTotal(), reqDTO);
        return ApiResultDTO.success(poPageResult);
    }

    @Override
    public ApiResultDTO addFile(AddUploadFileReqDTO reqDTO, MultipartFile file) {

        if(file == null){
            throw new CustomRunTimeException(ResultCodeEnum.PARAM_NOT_EXIST);
        }

        String url = this.fileUtils(file, reqDTO.getType());
        YhjFilePO po = new YhjFilePO();
        po.setName(reqDTO.getName());
        po.setType(reqDTO.getType());
        po.setUrl(url);
        yhjFileMapper.insert(po);
        return ApiResultDTO.success(po.getId());
    }

    public String fileUtils(MultipartFile logo, FileTypeEnum type){
        String[] split = logo.getOriginalFilename().split("\\.");
        String fileName =   UUID.randomUUID().toString() + ScalarUtil.DEFAULT_DIAN + split[split.length - 1];
        File file1 = new File(LlConstants.YHJ_FILE_PATH + type.ordinal() + fileName);
        try {
            logo.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return LlConstants.YHJ_FILE_HTTP_URL_PREFIX + type.ordinal() + fileName;
    }
}
